package com.zerosky.crm.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zerosky.crm.po.Employee;
import com.zerosky.crm.po.PersonalWorkPlan;
import com.zerosky.crm.service.PersonalWorkPlanService;
import com.zerosky.crm.util.BaseAction;
import com.zerosky.crm.util.PageInfo;

@Controller
public class PersonalWorkPlanController extends BaseAction {
	private Logger logger = LoggerFactory.getLogger(PersonalWorkPlanController.class); // 日志

	@Resource(name = "personalWorkPlanService")
	private PersonalWorkPlanService personalWorkPlanService; 
	
	/**
	 * 跳转个人工作计划界面
	 * @param id 主键ID
	 * @param request
	 * @param session
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/personalWorkPlan.do")
	public ModelAndView personalWorkPlan(String id, 
										 HttpServletRequest request, 
										 HttpSession session,
										 HttpServletResponse response) throws Exception {
		ModelAndView m = new ModelAndView("WEB-INF/jsp/user/personalWorkPlanList");
		return m;
	}
	
	/**
	 * 获取个人工作计划列表
	 * @param request
	 * @param session
	 * @param response
	 * @param limit 	页码数据数
	 * @param page		页码
	 * @param planTitle 计划标题
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/personalWorkPlanList.do")
	@ResponseBody
	public Map<String, Object> tripList(HttpServletRequest request, 	
										HttpSession session, 
										HttpServletResponse response,
										Integer limit, 
										Integer page, 
										String planTitle) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>(); // 定义返回map集合
		Employee user = (Employee) session.getAttribute("loginUser");// 获取登录用户信息
		String userId = user.getEmployeeId();
		int roleId = user.getRole().getId();
		Map<String, Object> param = new HashMap<String, Object>();// 定义条件集合
		/*if(roleId == 7||roleId == 8||roleId ==9){
		}*/
		param.put("employeeId", userId);
		param.put("method", "allPersonalWorkPlanList");
		PageInfo<PersonalWorkPlan> pageInfo = new PageInfo<PersonalWorkPlan>();
		try {
			pageInfo.setPageNo(page);
			pageInfo.setPageSize(limit);
			if (Strings.isNotEmpty(planTitle)) {
				param.put("planTitle", planTitle);
			}
			json.put("flag", "0");
			PageInfo<PersonalWorkPlan> pageRole = personalWorkPlanService.getPersonalWorkPlanList(param, pageInfo);
			json.put("total", pageRole.getPageCount());
			json.put("data", pageRole.getList());
			json.put("flag", "1");

		} catch (Exception e) {
			json.put("flag", "-1");
			logger.error("获取数据失败，原因：" + e);
		}
		return json;
	}
	
	/**
	 * 添加个人工作计划
	 * @param personalWorkPlan 对象
	 * @param request
	 * @param session
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/insertPersonalWorkPlan.do")
	@ResponseBody
	public Map<String, Object> insertPersonalWorkPlan(PersonalWorkPlan personalWorkPlan,
									   				  HttpServletRequest request,
									   				  HttpSession session, 
									   				  HttpServletResponse response) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			Employee user = (Employee) session.getAttribute("loginUser");// 获取登录用户
			personalWorkPlan.setEmployeeId(user.getEmployeeId());// 写入创建用户名称
			personalWorkPlan.setCreateDate(new Date());
			personalWorkPlan.setCreatePeople(user.getName());// 创建人
			personalWorkPlanService.insertOrUpdate(personalWorkPlan, "新增");
			json.put("code", 1);
			json.put("msg", "添加成功");
		} catch (Exception e) {
			json.put("code", -1);
			logger.error("获取数据失败，原因：" + e);
		}
		return json;
	}
	
	/**
	 * 修改个人工作计划信息
	 * @param id 主键ID
	 * @param request
	 * @param session
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/modifyPersonalWorkPlan.do")
	@ResponseBody
	public Map<String, Object> modifyPersonalWorkPlan(HttpServletRequest request,
													  String planTitle,
													  String principal,
													  String planType,
													  String planContent,
													  String startDate,
													  String endDate,
													  int planId) {
		PersonalWorkPlan personalWorkPlan =new PersonalWorkPlan();
		personalWorkPlan.setPlanTitle(planTitle);
		personalWorkPlan.setPrincipal(principal);
		personalWorkPlan.setPlanType(planType);
		personalWorkPlan.setPlanContent(planContent);
		personalWorkPlan.setStartDate(startDate);
		personalWorkPlan.setEndDate(endDate);
		personalWorkPlan.setPlanId(planId);
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			Integer count = personalWorkPlanService.insertOrUpdate(personalWorkPlan, "修改");
			if (count > 0) {
				json.put("flag", 1);
				json.put("msg", "修改成功!");
			} else {
				json.put("flag", 0);
				json.put("msg", "修改失败!");
			}

		} catch (Exception e) {
			json.put("flag", -1);
			json.put("msg", "系统异常!");
			logger.error("获取数据失败，原因：" + e);
		}

		return json;
	}
	
	/**
	 * 修改前查询数据
	 * @param request
	 * @param chanceId
	 * @return
	 */
	@RequestMapping("/modifyPersonalWorkPlanQuery.do")
	@ResponseBody
	public Map<String, Object> modifyPersonalWorkPlanQuery(HttpServletRequest request, String planId) {
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			PersonalWorkPlan personalWorkPlan = personalWorkPlanService.getPersonalWorkPlanById(planId);
			if (personalWorkPlan == null) {
				json.put("flag", 0);
				json.put("msg", "数据出现异常,请稍后再试!");
			} else {
				json.put("flag", 1);
				json.put("personalWorkPlan", personalWorkPlan);
			}
		} catch (Exception e) {
			json.put("flag", -1);
			json.put("msg", "程序出错了!");
			logger.error("获取数据失败，原因：" + e);
		}
		return json;
	}
	
	/**
	 * 获取日期小于或等于当前时间的工作计划
	 * @param request
	 * @param session
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/todayPlan.do")
	@ResponseBody
	public Map<String, Object> toDayPlanList(HttpServletRequest request,
											 HttpSession session,
											 HttpServletResponse response) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>(); // 定义返回map集合
		Employee user = (Employee) session.getAttribute("loginUser");// 获取登录用户信息
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String employeeId = user.getEmployeeId();
		int roleId = user.getRole().getId();
		Map<String, Object> param = new HashMap<String, Object>();// 定义条件集合
		/*if(roleId == 7||roleId == 8||roleId ==9){
		}*/
		param.put("employeeId", employeeId);
		param.put("startDate",sdf.format(new Date()));
		PageInfo<PersonalWorkPlan> pageInfo = new PageInfo<PersonalWorkPlan>();
		try {
			PageInfo<PersonalWorkPlan> pageRole = personalWorkPlanService.getTodayPersonalWorkPlanList(param, pageInfo);
			json.put("data", pageRole.getList());
			json.put("flag", "1");

		} catch (Exception e) {
			json.put("flag", "-1");
			logger.error("获取数据失败，原因：" + e);
		}
		return json;

	}
	/**
	 * 删除记录
	 * @param request
	 * @param session
	 * @param response
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/deletePersonalWorkPlan.do")
	@ResponseBody
	public Map<String, Object> deletePersonalWorkPlan(HttpServletRequest request,
													  HttpSession session,
													  HttpServletResponse response,
													  int id) throws Exception {
		Map<String, Object> map = new HashMap<>();
		try {
			Integer flag = personalWorkPlanService.deletePersonalWorkPlan(id);
			if (flag > 0) {
				map.put("msg", "删除成功");
				map.put("code", "success");
			} else {
				map.put("msg", "删除失败，数据异常");
				map.put("code", "error");
			}
		} catch (Exception e) {
			map.put("msg", "删除失败，请稍后再试");
			map.put("code", "error");
			logger.error("获取数据失败，原因：" + e);
		}
		return map;
	}
}
